package com.siren.tank.group.image;

import com.siren.tank.*;
import com.siren.tank.fire.FireStrategy;
import com.siren.tank.fire.ThreeDirctionFire;
import com.siren.tank.group.BaseTank;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Map;

/**
 * @author : wangjichao
 * @description:
 * @date : 2020/7/29 10:32
 */
public class GoodTank extends BaseTank {

    private static final int SPEED = 10;

    private boolean moving = false;

    private FireStrategy fireStrategy = new ThreeDirctionFire();

    public GoodTank(int x, int y, Dir dir) {
        super(x, y, dir);
        setGroup(Group.GOOD);
    }

    public boolean isMoving() {
        return this.moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    @Override
    public void control(Map<Integer, Boolean> keyEvents) {

        boolean bL = keyEvents.get(KeyEvent.VK_LEFT) == null ? false : keyEvents.get(KeyEvent.VK_LEFT);
        boolean bR = keyEvents.get(KeyEvent.VK_RIGHT) == null ? false : keyEvents.get(KeyEvent.VK_RIGHT);
        boolean bU = keyEvents.get(KeyEvent.VK_UP) == null ? false : keyEvents.get(KeyEvent.VK_UP);
        boolean bD = keyEvents.get(KeyEvent.VK_DOWN) == null ? false : keyEvents.get(KeyEvent.VK_DOWN);
        boolean ctrl = keyEvents.get(KeyEvent.VK_CONTROL) == null ? false : keyEvents.get(KeyEvent.VK_CONTROL);

        if(!bL && !bR && !bU && !bD) {
            this.setMoving(false);
        } else {
            this.setMoving(true);
            if(bL) {
                this.setDir(Dir.LEFT);
            }
            if(bR) {
                this.setDir(Dir.RIGHT);
            }
            if(bU) {
                this.setDir(Dir.UP);
            }
            if(bD) {
                this.setDir(Dir.DOWN);
            }
        }
        if (ctrl) {
            this.fire(new ThreeDirctionFire());
        }
    }

    @Override
    public void draw(Graphics g) {
        switch (getDir()) {
            case LEFT:
                g.drawImage(ResourceMgr.tankL, getX(), getY(), null);
                break;
            case RIGHT:
                g.drawImage(ResourceMgr.tankR, getX(), getY(), null);
                break;
            case UP:
                g.drawImage(ResourceMgr.tankU, getX(), getY(), null);
                break;
            case DOWN:
                g.drawImage(ResourceMgr.tankD, getX(), getY(), null);
                break;
        }
        move();
    }

    private void move() {
        if (isMoving()) {
            switch (getDir()) {
                case LEFT:
                    setX(getX() - SPEED);
                    break;
                case RIGHT:
                    setX(getX() + SPEED);
                    break;
                case UP:
                    setY(getY() - SPEED);
                    break;
                case DOWN:
                    setY(getY() + SPEED);
                    break;
                default:
                    break;
            }
        }
    }

    public void fire(FireStrategy fireStrategy) {
        fireStrategy.fire(this);
    }
}
